{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bbefe383-7913-4fbe-a1ed-9b307e8b788a",
   "metadata": {},
   "source": [
    "Chapter 07\n",
    "# 3个字符有放回取2个排列\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf30184b-6953-4ac4-b2f1-55aa59bede23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "string = 'abc'\n",
    "# 定义元素列表\n",
    "elements = list(string)\n",
    "# 指定重复次数\n",
    "repeat = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "970111ee-0015-47a2-ab10-046f256e8591",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成有放回排列\n",
    "permutations = itertools.product(elements, repeat=repeat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "56530467-a68e-4219-b31a-137a0f83c06b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa\n",
      "ab\n",
      "ac\n",
      "ba\n",
      "bb\n",
      "bc\n",
      "ca\n",
      "cb\n",
      "cc\n"
     ]
    }
   ],
   "source": [
    "# 遍历并打印所有排列\n",
    "for permutation_idx in permutations:\n",
    "    print(''.join(permutation_idx))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
